import { ref, watch, reactive, getCurrentInstance } from 'vue'
import {Search, Plus, Upload} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import tableComp from './component/tableComp'
import $button from '@/button'
import buttonAction from './component/buttonAction'
const proxy = getCurrentInstance();
const selectInit = reactive(['non', 'effective', 'invalid', 'reapplied'])

export const operationItem = reactive({
    defaultSearch: false,//默认搜索按钮
    defaultRefresh: false,//默认重置按钮
    customTableHeader: false,//
    SearchItem: [
        {
            code: 'orderNum',
            name: '关联订单',
            type: 'input',
        },
        {
            code: 'titleTypeCode',
            name: '票据类型',
            type: 'select',
            options: [
                { label: '企业发票', value: 'enterprise' },
                { label: '个人发票', value: 'person' }
            ]
        },
        {
            code: 'title',
            name: '发票抬头',
            type: 'input'
        },
        {
            code: 'taxIdentificationNumber',
            name: '纳税识别号',
            type: 'input'
        },
        {
            code: 'statusCodeList',
            name: '开票状态',
            type: 'select',
            valueInit: selectInit,
            closeClearable: true,
            options: [
                { label: '全部', value: selectInit },
                { label: '未开发票', value: ['non'] },
                { label: '已开发票', value: ['effective'] },
                { label: '已作废', value: ['invalid', 'reapplied'] }
            ]
        },
        {
            code: ['invoicingDateStart', 'invoicingDateEnd'],//
            startName: "开票日期", //默认是开始时间 type为 daterange生效
            rangeName: "", //默认是至 type为 daterange生效
            endName: "开票日期", //默认是结束时间 type为 daterange生效
            valueFormat: "", //默认是YYYY-MM-DD
            placeholder: "请选择日期",//默认是请选择日期type为date生效
            type: "daterange",//daterange或date
        }
    ],
    menuArray: [
        {
            show: true,
            type: 'primary',
            text: '查询',
            icon: Search,
            handle: () => {
            }
        },
        {
            show: true,
            type: 'primary',
            text: '导出',
            hasPermi: [$button.courseorderInvoiceexport|| null],
            icon: Upload,
            handle: () => {
            }
        },
    ]
})

export const tableItem = reactive({
    select: false,
    index: !true,
    tree: true,
    expandAll: true,
    align: 'center',
    column: [
        {
            label: '关联订单',
            prop: 'orderNum',
            align: 'center',
        },
        {
            label: '票据类型',
            prop: 'titleTypeCode',
            align: 'center',
            component: tableComp,
            param: 'titleTypeCode'
        },
        {
            label: '发票编号',
            prop: 'invoiceNumber',
            align: 'center'
        },
        {
            label: '发票抬头',
            prop: 'title',
            align: 'center',
        },
        {
            label: '纳税识别号',
            prop: 'taxIdentificationNumber',
            align: 'center'
        },
        {
            label: '开票金额(元)',
            prop: 'price',
            align: 'center',
        },
        {
            label: '开票日期',
            prop: 'invoicingDate',
            align: 'center',
        },
        {
            label: '开票状态',
            prop: 'statusCode',
            align: 'center',
            width: '100',
            component: tableComp,
            param: 'statusCode'
        },
        {
            label: '操作',
            prop: 'action',
            align: 'center',
            component: buttonAction
        }
    ],
    pageInfo: {
        request: {
            'orderNum': '',
            'titleTypeCode': '',
            'title': '',
            'taxIdentificationNumber': '',
            // 'statusCodeList': [],
            'invoicingDateStart': '',
            'invoicingDateEnd': '',
            'pageDomain': {
                'pageNum': '1',//分页页面数
                'pageSize': '10',//分页页面数据大小
                'orderByColumn': '',//搜索参数
                'isAsc': '',//搜索参数
                'reasonable': ''//搜索参数
            }
        },
        url: '/invoice/paging',//分页路由，不传的话需要传入自定义的分页函数paging 传入时定义的分页函数paging会失效 /invoice/paging
        method: 'POST',//分页请求方法，不传默认为POST
        // 分页函数paging
        paging: function () {
        }
    },
    // 分页数据 自定义分页函数paging时需要把值赋给tableData
    tableData: {
        total: '',//分页总计
        // 分页数据
        data: []
    },
    // 操作栏按钮
    btnArray: [
        {
            label: '查看',
            type: 'primary',
            labelEn: 'status',
            show: true,
            hasPermi: [$button.courseorderInvoiceview|| null],
            plain: true,
            text: true,
            // 按钮方法
            handle: function (row) {
            }
        },
        {
            label: '上传发票',
            type: 'primary',
            labelEn: 'status',
            show: true,
            hasPermi: [$button.courseorderInvoiceimport|| null],
            plain: true,
            text: true,
            // 按钮方法
            handle: function (row) {
            }
        },
        {
            label: '下载发票',
            type: 'primary',
            labelEn: 'status',
            show: true,
            hasPermi: [$button.courseorderInvoiceexport|| null],
            plain: true,
            text: true,
            // 按钮方法
            handle: function (row) {
            }
        },
        {
            label: '重新申请',
            type: 'primary',
            labelEn: 'status',
            show: true,
            hasPermi: [$button.courseorderInvoiceadd|| null],
            plain: true,
            text: true,
            // 按钮方法
            handle: function (row) {
            }
        },
        {
            label: '作废发票',
            type: 'danger',
            labelEn: 'status',
            show: true,
            hasPermi: [$button.courseorderInvoicedisable|| null],
            plain: true,
            text: true,
            // 按钮方法
            handle: function (row) {
            }
        },
    ],
    moreBtnArrayText: '',
    moreBtnArray: [],
    // 选择框触发
    selectChange: function (row) {
    },
    tableRowClassName: function (row) {
        if (row.row.statusCode === 'exception') {
            return 'warning-row';
        }
        return '';
    },
}
)

// 发票详情
export function invoiceDetailApi(id) {
    return request({
        url: '/invoice/' + id,
        method: 'GET',
    })
}
//上传发票
export function invoiceUploadApi(dataRequest) {
    return request({
        url: '/invoice/invoicing',
        method: 'PUT',
        data: dataRequest
    })
}
//作废发票
export function invoiceInvalidApi(id) {
    return request({
        url: '/invoice/discard/' + id,
        method: 'PUT'
    })
}
//重新申请
export function invoiceReapplyApi(id) {
    return request({
        url: '/invoice/reapply/' + id,
        method: 'PUT'
    })
}



